//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.vere.orm.log;

import com.vere.orm.db.Configuration;
import com.vere.orm.util.CDate;
import com.vere.orm.util.CFile;
import java.util.Calendar;
import java.util.Date;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

public abstract class ILog {
    public static Queue<String> logQueue = new ConcurrentLinkedQueue();
    public static final String DEBUG = "debug";

    public ILog() {
    }

    public void debug(String operateType, String sql) {
        if (Configuration.isShowSql()) {
            StringBuffer sb = new StringBuffer();
            sb.append(CLogDate.getLogTime()).append(" | ").append(operateType).append(" | ").append(sql);
            System.out.println(sb.toString());
        }

    }

    public void debug(String operateType, String sql, Object[] params) {
        if (Configuration.isShowSql()) {
            sql = this.parseSQL(sql, params);
            StringBuffer sb = new StringBuffer();
            sb.append(CLogDate.getLogTime()).append(" | ").append(operateType).append(" | ").append(sql);
            System.out.println(sb.toString());
        }

    }

    public void saveLogToFile() {
        String sql = (String)logQueue.poll();

        for(int num = 1; sql != null && (long)num < Configuration.getLogBatchSize(); ++num) {
            sql = (String)logQueue.poll();
            if (sql == null) {
                break;
            }

            String logPath = Configuration.getLogDirectory() + CFile.getFileSeparator() + "log" + CFile.getFileSeparator() + CDate.getCurrentDate() + ".log";
            CFile.logAppend(sql, logPath);
        }

    }

    public void flushAllLogToFile() {
        String sql = (String)logQueue.poll();

        while(sql != null) {
            sql = (String)logQueue.poll();
            if (sql == null) {
                break;
            }

            String logPath = Configuration.getLogDirectory() + CFile.getFileSeparator() + "log" + CFile.getFileSeparator() + CDate.getCurrentDate() + ".log";
            CFile.logAppend(sql, logPath);
        }

    }

    protected String parseSQL(String sql, Object[] params) {
        for(int i = 0; params != null && i < params.length; ++i) {
            Object param = params[i];
            String askSign = "[?]";
            if (param instanceof Integer) {
                sql = sql.replaceFirst(askSign, param.toString());
            } else if (param instanceof Long) {
                sql = sql.replaceFirst(askSign, param.toString());
            } else if (param instanceof Float) {
                sql = sql.replaceFirst(askSign, param.toString());
            } else if (param instanceof Double) {
                sql = sql.replaceFirst(askSign, param.toString());
            } else if (param instanceof Date) {
                sql = sql.replaceFirst(askSign, "'" + CDate.parseDateTime((Date)param) + "'");
            } else if (param instanceof Calendar) {
                sql = sql.replaceFirst(askSign, "'" + CDate.parseDateTime((Calendar)param) + "'");
            } else if (params[i] instanceof String) {
                sql = sql.replaceFirst(askSign, "'" + param.toString() + "'");
            } else {
                sql = sql.replaceFirst(askSign, "'" + param.toString() + "'");
            }
        }

        return sql;
    }

    public static Queue<String> getLogQueue() {
        return logQueue;
    }
}
